<script setup>
// 导入模块
import {ref, computed} from "vue";
// 模拟数据库
let goods = ref([
  {
    id: 1,
    goodsName: "瑞克5代",
    goodsPrice: 100,
    goodsNum: 10
  },
  {
    id: 2,
    goodsName: "绝版/瑞克5代",
    goodsPrice: 800,
    goodsNum: 10
  },
  {
    id: 3,
    goodsName: "芙蓉王",
    goodsPrice: 25,
    goodsNum: 10
  },
])

// 设置加减的函数
const add = function (id) {
  // goods.value[id].goodsNum++
  goods.value[id - 1].goodsNum++
}
const reduce = function (id) {
  // goods.value[id].goodsNum--
  goods.value[id - 1].goodsNum--
  if (goods.value[id - 1].goodsNum < 0) {
    goods.value[id - 1].goodsNum = 0
    alert("数量不能小于0")
  }
}
// 删除数组
const del = function (id) {
  goods.value.splice(id - 1, 1)
}
// 总价格
// 改为计算属性实现
const totalPrice = computed(() => {
  return goods.value.reduce((sum, item) => sum + item.goodsPrice * item.goodsNum, 0);
});

</script>
<template>
  <div class="container">
    <table class="goods-table">
      <thead>
      <tr class="table-header">
        <th>序号</th>
        <th>商品名</th>
        <th>价格</th>
        <th>数量</th>
        <th>操作</th>
      </tr>
      </thead>
      <tbody>
      <tr v-for="item in goods" :key="item.id" class="table-row">
        <td>{{ item.id }}</td>
        <td class="goods-name">{{ item.goodsName }}</td>
        <td class="price">¥{{ item.goodsPrice }}</td>
        <td>
          <div class="quantity-control">
            <button class="btn minus" @click="reduce(item.id)">-</button>
            <input
                type="number"
                
                v-model.number="item.goodsNum"
                class="quantity-input"
                min="0"
            >
            <button class="btn plus" @click="add(item.id)">+</button>
          </div>
        </td>
        <td>
          <button class="btn delete" @click="del(item.id)">删除</button>
        </td>
      </tr>
      <tr class="summary-row">
        <td colspan="3" class="total-label">总价格：</td>
        <td colspan="2" class="total-price">¥{{ totalPrice }}</td>
      </tr>
      <tr>
        <td colspan="5" class="checkout-cell">
          <button class="checkout-btn">
            立即结算 ({{ goods.length }}件商品)
          </button>
        </td>
      </tr>
      </tbody>
    </table>
  </div>
</template>
<style scoped>
.container {
  padding: 2rem;
  max-width: 1000px;
  margin: 0 auto;
}

.goods-table {
  width: 100%;
  border-collapse: collapse;
  box-shadow: 0 1px 3px rgba(0,0,0,0.1);
  background: white;
  border-radius: 8px;
  overflow: hidden;
}

.table-header {
  background-color: #2c3e50;
  color: white;
}

.table-header th {
  padding: 1rem;
  text-align: left;
}

.table-row {
  border-bottom: 1px solid #ecf0f1;
}

.table-row:hover {
  background-color: #f8f9fa;
}

.table-row td {
  padding: 1rem;
  vertical-align: middle;
}

.goods-name {
  color: #2c3e50;
  font-weight: 500;
}

.price {
  color: #e74c3c;
  font-weight: bold;
}

.quantity-control {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.btn {
  padding: 0.5rem 1rem;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  transition: all 0.3s ease;
}

.btn:hover {
  transform: translateY(-1px);
  box-shadow: 0 2px 5px rgba(0,0,0,0.1);
}

.minus {
  background-color: #95a5a6;
  color: white;
}

.plus {
  background-color: #3498db;
  color: white;
}

.delete {
  background-color: #e74c3c;
  color: white;
}

.quantity-input {
  width: 60px;
  padding: 0.5rem;
  text-align: center;
  border: 1px solid #bdc3c7;
  border-radius: 4px;
  font-size: 1rem;
}

.quantity-input:focus {
  outline: none;
  border-color: #3498db;
  box-shadow: 0 0 0 2px rgba(52,152,219,0.2);
}

.summary-row {
  background-color: #f8f9fa;
}

.total-label {
  font-weight: bold;
  text-align: right;
  padding-right: 1rem;
}

.total-price {
  color: #27ae60;
  font-size: 1.2rem;
  font-weight: bold;
}

.checkout-cell {
  text-align: right;
  padding: 1rem;
}

.checkout-btn {
  background-color: #27ae60;
  color: white;
  padding: 1rem 2rem;
  border: none;
  border-radius: 25px;
  font-size: 1.1rem;
  cursor: pointer;
  transition: all 0.3s ease;
}

.checkout-btn:hover {
  background-color: #219a52;
  transform: translateY(-2px);
  box-shadow: 0 3px 8px rgba(39,174,96,0.3);
}

@media (max-width: 768px) {
  .container {
    padding: 1rem;
  }

  .quantity-control {
    flex-direction: column;
  }

  .btn {
    width: 100%;
  }

  .quantity-input {
    width: 100%;
  }
}
</style>